#ifndef AppManager_H
#define AppManager_H

#include <QObject>
#include <QSettings>

#include "logger.h"
#include "my_defs.h"

#include "dbworker.h"
#include "styleloader.h"
#include "stylesdialog.h"
#include "styleiddelegate.h"
#include "mainwindow.h"
#include "treeitem.h"
#include "treemodel.h"
#include "settingsform.h"
#include "recordform.h"


/* - - - - - - - - - - - - - - - - - - - - - - - - -*
  Main singletone, controlls MainWindow, DBWorker,
  StyleLoader, SettingsForm and so on.
 - - - - - - - - - - - - - - - - - - - - - - - - - -*/

class AppManager : public QObject
{
    Q_OBJECT
public:
    static void CreateInstance();
    static void DestroyInstance();
    static AppManager *instance();

    DBWorker     *dbWorker();

    StyleIdDelegate *styleIdDelegate4Tree() { return mStyleIdDelegate4Tree; }
    StyleIdDelegate *styleIdDelegate4Tab() { return mStyleIdDelegate4Tab; }

    StyleLoader  *styleLoader() { return mStyleLoader; }
    StylesDialog *stylesDialog(){ return mStyleDialog; }
    MainWindow   *mainWindow()  { return mMainWindow; }
    QSettings    *appSettings() { return mAppSettings; }
    SettingsForm *settingsForm(){ return mSettingsForm; }
    RecordForm *recordForm(){ return mRecordForm; }

    void setup();
    void createDelegateForStyles();

    int stylesLoaded() const { return mStylesLoaded; }
    void setStylesLoaded(const int st) { mStylesLoaded = st; }

    // int -- styleId
    QMap<int, Style> mStyles; // TODO: *


private:
    AppManager();
    ~AppManager();
    static AppManager *mInstance;

    void saveSettings();
    void setupSignals();

    DBWorker     *mDbw;
    StyleLoader  *mStyleLoader;
    StyleIdDelegate *mStyleIdDelegate4Tree;
    StyleIdDelegate *mStyleIdDelegate4Tab;

    // UI
    StylesDialog *mStyleDialog;
    MainWindow   *mMainWindow;
    SettingsForm *mSettingsForm;

    RecordForm *mRecordForm;

    QSettings *mAppSettings;

    int mStylesLoaded; // TODO: make as property
};

#define appManager (AppManager::instance())

#endif // AppManager_H
